package org.shadow.core.key.database;

/**
 * 
 * @author zhouw
 *
 */
public class MysqlSequenceTable extends DatabaseSequenceTable {
	
	/**
	 * 分布式环境下，多个应用都执行插入初始化的语句会导致出现重复,应手工初始
	 */
	@Override
	public String getInsertSql() {
		StringBuilder builder = new StringBuilder();
		builder.append("insert into " + this.getTableName() + " ( " + this.getKeyTypeColumn());
		builder.append("," + this.getKeyValueColumn() + "," + this.getUpdateTimeColumn() + ") values (?, ?, ?)"); 
		return builder.toString();
	}
	
	@Override
	public String getSelectSql() {
		StringBuilder builder = new StringBuilder();
		builder.append("select " + this.getKeyValueColumn() + " from " + this.getTableName());
		builder.append(" where " + this.getKeyTypeColumn() + "= ?");
		if (this.getLockType() != 0) {
			builder.append(" for update");
		}
		
		return builder.toString();
	}

	@Override
	public String getUpdateSql() {
		StringBuilder builder = new StringBuilder();
		builder.append("update " + this.getTableName());
		builder.append(" set " + this.getKeyValueColumn() + " = ?," + this.getUpdateTimeColumn() + " = now()");
		builder.append(" where " + this.getKeyTypeColumn() + "= ? and " + this.getKeyValueColumn() + "= ?");
		return builder.toString();
	}



}
